<?php
/**
 * 钩子文件（注意别删）
 * 利用web hooks
 * 目前
 * */
require(__DIR__ . '/Application/Common/Common/Mail.php');
$developInfo = require(__DIR__ . '/Application/Common/Conf/config.php');
$password    = !empty($developInfo['PASSWORD']) ? $developInfo['PASSWORD'] : '';
$developInfo = !empty($developInfo['DEVELOPMENT']) ? $developInfo['DEVELOPMENT'] : [];

$hookData = $_REQUEST['hook'];
$hookData = json_decode($hookData, true);

if (md5($hookData['password']) != $password) {
	$cmd = 'echo error_password >> ../git_log/log.txt';
	exec($cmd);
	die('ERROR');
} else {
	$cmd = 'git clean -f .>> ../git_log/log.txt';
	exec($cmd);
	$cmd = 'git pull >> ../git_log/log.txt';
	exec($cmd, $output);
	$body = 'Git操作了一次并无法识别是何种操作！';
	parseHook($hookData, $body);
	$date = toDay();
	foreach ($developInfo as $person) {
		if (think_send_mail($hookData['password'], $person['email'], $person['name'], '自动邮件', $body)) {
			exec("echo '$date:send Ok' >> ../git_log/email_status.txt");
		} else {
			exec("echo '$date:send False' >> ../git_log/email_status.txt");
		}
	}
	die('SUCCESS');
}

function parseHook($hookData, &$body)
{
	switch ($hookData['hook_name']) {
		case 'push_hooks' :
			$pushData = $hookData['push_data'];
			if (!empty($pushData['commits']) && !empty($pushData['user'])) {
				$name = $pushData['user']['name'];
				$mail = $pushData['user']['email'];
				$time = $pushData['user']['time'];

				$body = '<h1>Operation is Commit</h1>'
					. "<p>From $name $mail at $time</p>";
				$i    = 1;
				$sum  = $pushData['total_commits_count'];
				$body .= "<p>$sum Commit</p>";
				foreach ($pushData['commits'] as $data) {
					$message = $data['message'];
					$time    = $data['timestamp'];
					$url     = $data['url'];
					$authorName  = $data['author']['name'];
					$authorEmail = $data['author']['email'];
					$authorTime  = $data['author']['time'];
					$body .= "<p>$i. $message $url</p>";
					$body .= "<p>Author: $authorName $authorEmail at $authorTime</p>";
				}
			}
			break;

		case 'issue_hooks' :
			$hookData  = $hookData['push_data'];
			$title     = $hookData['title'];
			$state     = $hookData['state'];
			$assignee  = $hookData['assignee'];
			$milestone = $hookData['milestone'];
			$body  = "<h1>Operation is Issue</h1>";
			$body .= "<h3>$title</h3>";
			$body .= "<p>state: $state</p>";
			$body .= "<p>assignee : $assignee</p>";
			$body .= "<p>milestone: $milestone</p>";
			break;

		case 'note_hooks' :
			$hookData    = $hookData['push_data'];
			$note        = $hookData['note'];
			$noteType    = $hookData['noteable_type'];
			$authorName  = $hookData['author']['user_name'];
			$authorEmail = $hookData['author']['email'];
			$body  = "<h1>$noteType 的评论：</h1>";
			$body .= "<p>$authorName $authorEmail :</p>";
			$body .= "<p>$note</p>";
			break;

		case 'merge_request_hooks' :
			$body = "<h1>一个分支合并,具体自行查看</h1>";
			break;

		default :
			unset($hookData['password']);
			$body .= "<p>" . json_encode($hookData) . "</p>";
			break;
	}
}

function toDay()
{
	if(date_default_timezone_get() != "1Asia/Shanghai") {
		date_default_timezone_set("Asia/Shanghai");
	}
	$date = date('Y:m;d h:i', time());
	return $date;
}



